package com.bookStore.service;

import java.util.List;

import com.bookStore.dao.UserDao;
import com.bookStore.domain.User;

public class UserService {
	
	public User checkLogin(String email, String password){
		User user = null;
		UserDao userDao = new UserDao();
		user = userDao.checkLogin(email, password);
		return user;
	}
	
	public Boolean isExistUser(String email){
		Boolean isExist = false;
		UserDao userDao = new UserDao();
		isExist = userDao.isExistUser(email);
		return isExist;
	}
	
	public Boolean registerUser(String email, String password) {
		Boolean isSuccess = false;
		UserDao userDao = new UserDao();
		isSuccess = userDao.registerUser(email, password);
		return isSuccess;
	}
	
	 // 得到用户表总记录数
    public int getRowCount() throws Exception {
        int rowCount = 0;
        try {
            UserDao userdao = new UserDao();
            rowCount = userdao.getRowCount();
        } catch (Exception e) {
            e.getMessage();
        }
        return rowCount;
    }
 
    // 得到用户表总页数
    public int getPageCount(int pageSize) throws Exception {
        int pageCount = 0;
        int rowCount = 0;
        try {
            rowCount = getRowCount();
            if (rowCount % pageSize == 0) {
                pageCount = rowCount / pageSize;
            } else {
                pageCount = rowCount / pageSize + 1;
            }
        } catch (Exception e) {
            e.getMessage();
        }
        return pageCount;
    }
 
    // 获取全部用户信息
    public List<User> getPagingUser(int startRow, int pageRows) {
        List<User> users = null;
        try {
            UserDao userdao = new UserDao();
            users = userdao.getPagingUser(startRow, pageRows);
        } catch (Exception e) {
            e.getMessage();
            users = null;
        }
        return users;
    }
 
    // 删除用户
    public boolean deleteUser(int id) {
        boolean result = false;
        try {
            UserDao userdao = new UserDao();
            userdao.deleteUser(id);
            result = true;
        } catch (Exception e) {
            e.getMessage();
            result = false;
        }
        return result;
    }
    
 // 编辑用户
    public boolean update(int id, String password) {
        boolean result = false;
        try {
            UserDao userdao = new UserDao();
            userdao.update(id, password);
            result = true;
        } catch (Exception e) {
            e.getMessage();
            result = false;
        }
        return result;
    }

}
